package cn.edu.jxau.test;

import java.util.Arrays;

/**
 * 动态生长
 * @author 付大石
 */
public class UnionFind {

    public static void main(String[] args) {
        
        UnionFind uf = new UnionFind();
        uf.union(4,3);
        System.out.println(uf);
        uf.union(3,8);
        System.out.println(uf);
        uf.union(6,5);
        System.out.println(uf);
        uf.union(9,4);
        System.out.println(uf);
        uf.union(2,1);
        System.out.println(uf);
        uf.union(8,9);
        System.out.println(uf);
        uf.union(5,0);
        System.out.println(uf);
        uf.union(7,2);
        System.out.println(uf);
        uf.union(6,1);
        System.out.println(uf);
        uf.union(1,0);
        System.out.println(uf);
        uf.union(6,7);
        System.out.println(uf);
    }
    
    /**
     * 触点集
     */
    private int[] idArr;

    /**
     * 权重
     */
    private int[] weightArr;

    /**
     * 连通分量个数
     */
    private int count;

    /**
     * 初始大小
     */
    private static final int INIT_CAPACITY = 5;

    public UnionFind() {

        this.count = INIT_CAPACITY;
        idArr = new int[INIT_CAPACITY];
        weightArr = new int[INIT_CAPACITY];
        for (int i = 0; i < idArr.length; i++) {
            idArr[i] = i;
            weightArr[i] = 1; // 初始时各个触点权重为1

        }
    }

    public boolean connected(int p, int q) {
        return find(p) == find(q);
    }

    public int find(int p) {

        if (p >= idArr.length) {
            resize(p+1);
        }
        while (idArr[p] != p) {
            p = idArr[p];
        }
        return p;
    }

    public int count() {
        return count;
    }

    public void union(int p, int q) {

        int max = p > q ? p : q;
        if(max >= idArr.length) {
            resize(max+1);
        }
        int pRoot = find(p); // p连通分量
        int qRoot = find(q); // q连通分量
        if (pRoot == qRoot) {
            return;
        }

        // 连接两个连通分量 //
        if (weightArr[pRoot] < weightArr[qRoot]) {
            idArr[pRoot] = qRoot; // 将p连通分量连到q连通分量上
            weightArr[qRoot] += weightArr[pRoot];
        } else {
            idArr[qRoot] = pRoot; // 将q连通分量连到p连通分量上
            weightArr[pRoot] += weightArr[qRoot];
        }
        count--;
    }

    private void resize(int newCapacity) {
        
        System.out.printf("resize to %d\n",newCapacity);
        int[] tempIdArr = new int[newCapacity];
        int[] tempWeightArr = new int[newCapacity];
        for(int i=0; i<idArr.length; i++) {
            tempIdArr[i] = idArr[i];
            tempWeightArr[i] = weightArr[i];
        }
        for(int i=idArr.length; i<newCapacity; i++) {
            tempIdArr[i] = i;
            tempWeightArr[i] = 1;
        }
        count += (newCapacity-idArr.length);
        idArr = tempIdArr;
        weightArr = tempWeightArr;
        
    }

    @Override
    public String toString() {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("count:").append(count).append("\t");
        stringBuilder.append(Arrays.toString(idArr));
        return stringBuilder.toString();
    }
}
